/**
 * Project Name:create_direct_pay_by_user-JAVA-UTF-8
 * File Name:MD5.java
 * Package Name:com.alipay.util
 * Date:2014年6月5日下午6:12:41
 * Copyright (c) 2014, 畅游公司  All Rights Reserved.
 */

package com.ctakit.sdk.util;

import android.text.TextUtils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * ClassName:MD5 <br/>
 * Function: MD5签名工具类. <br/>
 * Date: 2014年6月5日 下午6:12:41 <br/>
 *
 * @author zhangtongzheng
 * @see
 * @since JDK 1.6
 */
public final class MD5 {

    public static final String DEFAULT_CHARSET_NAME = "UTF-8";
    private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    private static final char[] DIGITS_UPPER = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    /**
     * sign:采用UTF-8字符集对给定字符进行MD5签名. <br/>
     *
     * @param data
     * @return
     * @author zhangtongzheng
     * @since JDK 1.6
     */
    public static String sign(String data) {

        return md5Hex(getBytes(data, DEFAULT_CHARSET_NAME));
    }

    /**
     * sign:采用给定字符集对给定字符进行MD5签名. <br/>
     *
     * @param data
     * @param charset
     * @return
     * @author zhangtongzheng
     * @since JDK 1.6
     */
    public static String sign(String data, String charset) {

        return md5Hex(getBytes(data, charset));
    }

    public static String md5Hex(byte[] data) {
        return encodeHexString(md5(data));
    }

    public static byte[] md5(byte[] data) {
        return getMd5Digest().digest(data);
    }

    private static MessageDigest getMd5Digest() {
        return getDigest("MD5");
    }

    static MessageDigest getDigest(String algorithm) {
        try {
            return MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    public static String encodeHexString(byte[] data) {
        return new String(encodeHex(data));
    }

    public static char[] encodeHex(byte[] data) {
        return encodeHex(data, true);
    }

    public static char[] encodeHex(byte[] data, boolean toLowerCase) {
        return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
    }

    protected static char[] encodeHex(byte[] data, char[] toDigits) {
        int l = data.length;
        char[] out = new char[l << 1];

        int i = 0;
        for (int j = 0; i < l; i++) {
            out[(j++)] = toDigits[((0xF0 & data[i]) >>> 4)];
            out[(j++)] = toDigits[(0xF & data[i])];
        }
        return out;
    }

    /**
     * getBytes:根据给定字符集获取相应的字节. <br/>
     *
     * @param content
     * @param charset
     * @return
     * @throws java.security.SignatureException
     * @throws UnsupportedEncodingException
     * @author zhangtongzheng
     * @since JDK 1.6
     */
    private static byte[] getBytes(String content, String charset) {
        if (charset == null || TextUtils.isEmpty(charset)) {
            return content.getBytes();
        }
        try {
            return content.getBytes(charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,你目前指定的编码集是:"
                    + charset);
        }
    }

}
